package efr.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import efr.dao.EmailDao;
import efr.entity.Email;
import efr.util.HibernateUtil;

public class EmailDaoImpl implements EmailDao {

	private static Logger logger = Logger.getLogger(EmailDaoImpl.class);
	@Override
	public List<Email> getAllEmails() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Email getEmail4Newsletter() {
		List<Email> listEmails = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		listEmails = session.createQuery("FROM Email E WHERE E.confirmare=1 order by E.id asc").list();
		session.getTransaction().commit();
		if (listEmails != null) {
			return listEmails.get(0);
		}else{
			return null;
		}

//	@SuppressWarnings("unchecked")
//	@Override
//	public List<Comentariu> getAllComentarii() {
//		List<Comentariu> listComentarii = null;
//		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//		session.beginTransaction();
//		listComentarii = session.createQuery("FROM Comentariu C WHERE C.status='N'").list();
//		for (Comentariu comentariu : listComentarii) {
//			Hibernate.initialize(comentariu.getPoezie());
//		}
//		session.getTransaction().commit();
//		return listComentarii;
//	
	}
//
//	@Override
//	public Comentariu getComentariu(int id) {
//		Comentariu comentariu = null;
//		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//		session.beginTransaction();
//		comentariu = (Comentariu)session.get(Comentariu.class, id);
//		Hibernate.initialize(comentariu.getPoezie());
//		session.getTransaction().commit();
//		return comentariu;
//	}
//
//	@Override
//	public void updateComentariu(Comentariu comentariu) {
//		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//		session.beginTransaction();
//		session.update(comentariu);
//		session.getTransaction().commit();
//		
//	}
//
//	@Override
//	public Integer addComentariu(Comentariu comentariu) {
//		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//		session.beginTransaction();
//		Integer id = (Integer)session.save(comentariu);
//		session.getTransaction().commit();
//		return id;
//	}
//
//	@Override
//	public void deleteComentariu(Comentariu comentariu) {
//		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//		session.beginTransaction();
//		session.delete(comentariu);
//		session.getTransaction().commit();
//	}

	@Override
	public void updateEmail(Email email) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		String hql = "update Email E set E.nume = :nume, E.prenume = :prenume, E.email = :email, E.obs = :obs, E.confirmare = :confirmare, E.code = :code, E.ipUnsubscribe = :ipUnsubscribe, E.dataUnsubscribe = :dataUnsubscribe where E.id = :id";
		Query query = session.createQuery(hql);
		query.setParameter("nume", email.getNume());
		query.setParameter("prenume", email.getPrenume());
		query.setParameter("email", email.getEmail());
		query.setParameter("obs", email.getObs());
		query.setParameter("confirmare", email.getConfirmare());
		query.setParameter("code", email.getCode());
		query.setParameter("ipUnsubscribe", email.getIpUnsubscribe());
		query.setParameter("dataUnsubscribe", email.getDataUnsubscribe());
		query.setParameter("id", email.getId());
		int result = query.executeUpdate();
		logger.debug("Rows affected: " + result);
		session.getTransaction().commit();
	}

	@Override
	public List<Email> findByEmail(String email) {
		List<Email> listEmails = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria emailCriteria = session.createCriteria(Email.class);
		emailCriteria.add(Restrictions.like("email", email, MatchMode.ANYWHERE));
		listEmails = emailCriteria.list();
		session.getTransaction().commit();
		return listEmails;
	}

	@Override
	public Email getEmail(int id) {
		Email email = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		email = (Email)session.get(Email.class, id);
		session.getTransaction().commit();
		return email;
	}
}
